{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "54784e09",
   "metadata": {},
   "source": [
    "### 字典的循环遍历"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5642de5b",
   "metadata": {},
   "source": [
    "* 学习文档<https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "420e597c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu11': 'Zz', 'stu12': 'Cc', 'stu13': 'Bb'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 新建字典\n",
    "info = { \n",
    "    \"stu11\":\"Zz\",\n",
    "    \"stu12\":\"Cc\",\n",
    "    \"stu13\":\"Bb\"\n",
    "}\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "77aff802",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu11\n",
      "stu12\n",
      "stu13\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历一个字典\n",
    "# i是item在字典循环中打印的是 key\n",
    "for i in info:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "da601223",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stu11 Zz\n",
      "stu12 Cc\n",
      "stu13 Bb\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历整个字典的 items \n",
    "# 这里的字典是无序的 所以不能使用枚举 \n",
    "for k,v in info.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "16675961",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'11': 'Zz', '12': 'Cc', '13': 'Bb'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实践1：把字典中的对象 取出数字，作为新的字典key\n",
    "new_info = {}\n",
    "for k,v in info.items():\n",
    "    # 用字典新增的方式创建字典\n",
    "    new_info[k[3:]] = v     # 切片的方法\n",
    "new_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "8cebd62f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'11': 'ZZ', '12': 'CC', '13': 'BB'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实践2： 把所有value的所有字母大写\n",
    "new_info = {}                   \n",
    "for k,v in info.items():\n",
    "    # 用字典新增的方式创建字典\n",
    "    new_info[k[3:]] = v.upper()  # vpper()方法可以把字符串类型中的字母型所有的小写变为大写   \n",
    "new_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "351e7e7c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{11: 'ZZ', 12: 'CC', 13: 'BB'}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 实践3： 让new_info 的key 由 '11'  ==> 11  有字符串转换为数值 用int\n",
    "new_info = {}\n",
    "for k,v in info.items():\n",
    "    # 用字典新增的方式创建字典\n",
    "    new_info[int(k[3:])] = v.upper()     \n",
    "new_info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65bb320e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
